<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>部门信息</title>
<style type="text/css">
	#logo {
		 background-image:url('..//image/base/logo.jpg');
  			background-repeat:no-repeat;
  		background-attachment:fixed;
  		background-position:right;
	}
.hello-button {  
  background: url(../image/index/dep.png) left top no-repeat!important;  
}
.page {
  	position:absolute;
  	bottom: 0px;
	left: 50px;
  }
</style>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/ext-all.css"></link>
<link rel="stylesheet" type="text/css" href="../ext/resources/css/xtheme-slate.css"></link>
<script src="../ext/adapter/ext/ext-base.js"></script>
<script src="../ext/ext-all.js"></script>
<script src="../ext/src/locale/ext-lang-zh_CN.js"></script>	
<script type="text/javascript">
			Ext.onReady(function(){
				Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);      
				function checkUserSessionStatus(conn,response,options){      
				    if(response.getResponseHeader('sessionstatus')){      
				    	parent.location.href='/xdj/index.jsp';   
				    }      
				}  
				//Ext.BLANK_IMAGE_URL='ext/resources/images/default/s.gif';
				var thenode;
				var deptForm ;
				var w;
				var treeloader = new Ext.tree.TreeLoader({   // dataUrl : 'tree.jsp'//这里可以不需要指定URL，在加载前事件响应里面设置 
            		
				});  
  
				var rootnode = new Ext.tree.AsyncTreeNode({  // 异步加载根节点 
					id : '2',  
					text : '大坝电厂',
					cls : 'folder',
					icon:'../image/public/folder_go.png'
				});  
				var tree=new Ext.tree.TreePanel({
					el : 'tree',// 将树形添加到一个指定的div中,非常重要！  
					border:false,
					cls:'logo4',
					//html:'<img src="../image/base/bg.jpg">',
					region : 'west',  
					//title : '功能菜单',  
					lines:true, //线段
					width : 300, 
					height:350,  
					split : true, 
					//autoHeight : true,  
					frame : true,// 美化界面    
					enableDD : false,// 是否支持拖拽效果  
					containerScroll : true,// 是否支持滚动条  
					rootVisible : true, // 是否隐藏根节点,很多情况下，我们选择隐藏根节点增加美观性  
					animate : true, // 动画效果  
					root : rootnode,
					loader : treeloader ,
					listeners : {
						'beforeload' : function(node) {
							treeloader.dataUrl = "depart_load?parentID=parentID_" +node.id+"_EQ";
						},
						'click' : function(node) {
							
							thenode = node ;
							Ext.Ajax.request({
							    url: 'depart_get',
							    params: {
							        departmentID: thenode.id
							    },
							    success: function(response){
							    	var result = Ext.util.JSON.decode(response.responseText);
							    	if(result.success==true) {
							    		selectForm.show();
							    		selectForm.form.setValues({
											'dept.departmentCode':result.departmentCode,
											'dept.depName':result.depName,
											'dept.depTel':result.depTel,
											'dept.remarks':result.remarks,
											'dept.departmentID':result.departmentID,
											'dept.parentID':result.parentID
										});
							    	}
							    }
							});
						}
					}
					
				// 树加载  
				});  
				tree.render();  // 渲染树形 
				/*var store=new Ext.data.JsonStore({
 				 	url: 'depart_load',
 				 	autoLoad : true,
 				 	//root: 'result',
					// idProperty:'id',
					//totalProperty:'totalProperty',
 				    fields: ["departmentCode", "depName","depTel","remarks"]
 				});//存储前两项的定义的东西，将其连接起来*/
 				selectForm=new Ext.form.FormPanel({
					frame:true,
					hidden:true,
					height:window.screen.height,
					buttonAlign:'top',
					labelAlign:'right',
					layout:'form',
					items:[{
						fieldLabel:'部门编码',
						xtype:'textfield',
						allowBlank:false,
						blankText:'编码不能为空',
						name:'dept.departmentCode'
					},{
						fieldLabel:'部门名称',
						xtype:'textfield',
						allowBlank:false,
						blankText:'名称不能为空',	
						name:'dept.depName'
					},{
						fieldLabel:'部门电话',
						xtype:'textfield',
						name:'dept.depTel'
					},{
						fieldLabel:'备注',
						xtype:'textarea',
						width:200,
						height:50,
						grow:true, //可自动增长
						preventScrollbars:true,  //不显示那个下拉条 必须与grow一起用
						name:'dept.remarks'
					},{
						xtype:'hidden',
						name:'dept.departmentID'
					}
					]
 				});
				function showWindown(thenode,urlString) {
					deptForm=new Ext.form.FormPanel({
						frame:true,
						height:200,
						buttonAlign:'top',
						labelAlign:'right',
						layout:'form',
						items:[{
							fieldLabel:'部门编码',
							xtype:'textfield',
							allowBlank:false,
							blankText:'编码不能为空',
							name:'dept.departmentCode'
						},{
							fieldLabel:'部门名称',
							xtype:'textfield',
							allowBlank:false,
							blankText:'名称不能为空',	
							name:'dept.depName'
						},{
							fieldLabel:'部门电话',
							xtype:'textfield',
							name:'dept.depTel'
						},{
							fieldLabel:'备注',
							xtype:'textarea',
							width:200,
							height:50,
							grow:true, //可自动增长
							preventScrollbars:true,  //不显示那个下拉条 必须与grow一起用
							name:'dept.remarks'
						},{
							xtype:'hidden',
							name:'dept.parentID',
							value:thenode.id
						},{
							xtype:'hidden',
							name:'dept.departmentID'
						}
						],
						buttons:[{
					       text: "确认",
					       handler:function(){ 
					    	   if ( deptForm.getForm().isValid() ){
					    	    deptForm.form.submit({   
					        	waitMsg : '正在提交数据....',   
					            url:urlString,   
					            method:'post',   
					            success:function(deptForm,action){  
					            	var result = Ext.util.JSON.decode(action.response.responseText);
					            	if(result.success==true) {
					            		//alert(deptForm.getForm().findField('dept.depName').getValue()) ;
					            		if(urlString=="depart_save"){
					            			
						            		var nodex = new Ext.tree.TreeNode({
						            				id : result.id,  
						        					text : result.name,  
						        					cls : 'folder' 
						            		});
											thenode.appendChild(nodex);
											thenode.reload() ;
											Ext.getBody().unmask();
					            		}else{
					            			thenode. setText(Ext.get("dept.depName").getValue()) ;
					            		}
					            		
										win.close() ;
					            		Ext.Msg.alert("提示","操作成功。") ;
					            	}else{
					            		Ext.Msg.alert("提示","操作失败。") ;
					            	}
					            }
					          }); 
					       }
					       }
					     },{
					            text: "取消",
					           	handler:function(){ 
					           		win.close() ;
					           		Ext.getBody().unmask();
					           	}
					            
					        }
					     ]
					});
					var win=new Ext.Window({
						//title:'增加部门',
						 width:400,
						 height:230,
						 items:[deptForm],
						 xtype:'form',
						 listeners:{"beforedestroy":function(obj)
			                 { 
							 Ext.getBody().unmask();  
			                    // obj.show(); return false; 
			                  }
						}  
					});
					win.show();
					//var nodex = new Ext.tree.TreeNode({text:'我是新的'});
					//thenode.appendChild(nodex);
				}
				var contextmenu=new Ext.menu.Menu({
					id:'contextmenu',
					items:[
{
							text:'增加',disabled:true,id:'cToolInsertDepartmentTreeBrotherNode',
							handler:function(node){
								Ext.getBody().mask();
								showWindown(thenode,"depart_save") ;
								//tree.enable() ;
							}
						},
						{
							text:'删除',disabled:true,id:'cToolDeleteDepartmentTreeNode',
							handler:function(node){
								Ext.Ajax.request({
									url:'depart_delete',
									success:function(){},
									params:{departmentID:thenode.id}
								});
								
								thenode.remove();
							}
						},
						{
							text:'修改',disabled:true,id:'cSaveDepartmentBox',
							handler:function(node){
								//tree.setDisabled(true);
								Ext.getBody().mask();
								Ext.Ajax.request({
								    url: 'depart_get',
								    params: {
								        departmentID: thenode.id
								    },
								    success: function(response){
								    	var result = Ext.util.JSON.decode(response.responseText);
								    	if(result.success==true) {
								    		showWindown(thenode,"depart_update") ;
								    		deptForm.form.setValues({
												'dept.departmentCode':result.departmentCode,
												'dept.depName':result.depName,
												'dept.depTel':result.depTel,
												'dept.remarks':result.remarks,
												'dept.departmentID':result.departmentID,
												'dept.parentID':result.parentID
											});
								    	}
								    }
								});
							}
						}
						
					]
				});
				
				tree.on('contextmenu',function(node,e){
					e.preventDefault();//屏蔽掉系统浏览器菜单
					node.select();
					thenode=node;
					contextmenu.showAt(e.getXY());
				}); 
				
				var form = new Ext.form.FormPanel({
					height:Ext.get("form").getWidth(),  
					html:'<p>部门信息树采用三层结构<br>第一层：企业或企业分厂<br>第二层：企业管理部门<br>第三层：子部门</p>',
					frame:true
				});
				
				 w= new Ext.Viewport({  
					layout : 'border', // 使用border布局  
					
					items:[{
						region:'west',
						border:false,
						width:200,
						height:Ext.get("form").getWidth(), 
						layout:'vbox',
						items:[tree,{height:140,width:300,items:[form]}]
					},{						
						region:'center',
						
						border:false,
						width:400,
						html:'<img src="../image/base/bg.jpg">',
						items:[selectForm,{hidden:true}]
					}],
					listeners:{
						'beforerender' : function(cmp) {
							// set role
							var store=new Ext.data.JsonStore({
								url: '../system/role_loadFunc?funcCode=ControlDepartment',
								autoLoad : true,
								fields: ["functioncode"]
							});
							store.load({
							    callback: function(records, options, success){
							    	var i=0;
							    	for(; i<records.length; i++) {
							    		var id = records[i].get("functioncode");
							    		//alert('d'+Ext.getCmp('56').text);
							    		if(Ext.getCmp(id)!=null) {
							    			Ext.getCmp(id).setDisabled(false);
							    		}
							        }
							    },
							    scope: store
							});
						}
					}
				 });  
			});
		</script> 
</head>
<body>
<div id="grid" ></div>
<div id="tree" style="width:100%;height:60%" ></div>
<div id="form" style="width:100%;height:100%"></div>
</body>
</html>